`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2022/03/07 16:18:12
// Design Name: 
// Module Name: tb_SM3_Top
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module tb_SM3_Top ;

reg clock;
reg rst_n;
reg [31:0] msg;
reg msg_valid;
reg is_last_word;
reg [1:0] is_last_word_byte;

wire sm3_ready;
wire [255:0] sm3_result ;
wire sm3_finished;

initial begin
    clock = 0;
    forever begin
        #5 clock = ~clock;
    end
end

initial begin
    rst_n = 0;
    #25 rst_n = 1;
end


//testcase 1---------------------------------------------------------------------------------------------------
//initial begin
//   msg = 0;
//   msg_valid = 0;
//   is_last_word = 0;
//   is_last_word_byte = 0;
//   #25
//   @(negedge clock) begin
//       msg = 32'h61626300;
//       msg_valid = 1;
//       is_last_word = 1;
//       is_last_word_byte = 2'b10;
//   end
//   @(negedge clock) begin
//       msg = 0;
//       msg_valid = 0;
//       is_last_word = 0;
//       is_last_word_byte = 0;
//   end
//   wait(sm3_finished)
//   #100
//   $finish;
//end

//always @(posedge clock) begin
//    if(SM3_U0.PAD0.a_block_ready) begin
//        $display("INFO : A BLOCK IS PADDED READY !");
//        $display("       MSG_PAD_BLOCK = %h",SM3_U0.PAD0.msg_padding);
//    end
        
//end
//always@(*) begin
//    if(SM3_U0.ECU0.block_comp_finished_out) begin
//    #1
//        $display("block compress finished!");
//        $display("round 63 compress result is: %64h @",SM3_U0.ECU0.Vi_out,$realtime);
//        if(SM3_U0.ECU0.Vi_out == 256'h1547e69b_2bfa5f60_c6d696bc_069ae2e2_e808f43b_4ac3cf08_caf04e66_3fb0a6ae)
//            $display("ECU PASS !");
//        else
//            $display("ERROR ! CHECK COMPRESS VALUE!");
//    end
//end
//always @(*) begin
//    if(sm3_finished) begin
//        #1
//        $display("SM3 PROCESSING FINISHED!");
//        $display("HASH VALUE IS %64h",sm3_result);
//        if(sm3_result == 256'h66c7f0f4_62eeedd9_d1f2d46b_dc10e4e2_4167c487_5cf2f7a2_297da02b_8f4ba8e0)
//            $display("SM3 TEST PASS !");
//        else
//            $display("ERROR ! CHECK HASH VALUE!");
//    end
//end

//---------------------------------------------------------------------------------------------------------------------------------------------
//testcase2
initial begin
    msg = 0;
    msg_valid = 0;
    is_last_word = 0;
    is_last_word_byte = 0;
    #25
    repeat(15) begin
        @(negedge clock) begin
        msg = 32'h61626364;
        msg_valid = 1;
       end
    end
    @(negedge clock) begin
        msg = 32'h61626364;
        msg_valid = 1;
        is_last_word = 1;
        is_last_word_byte = 2'b11;
    end
    @(negedge clock) begin
        msg = 0;
        msg_valid = 0;
        is_last_word = 0;
        is_last_word_byte = 0;
    end
    wait(sm3_finished)
    #500
    $finish;
end
always @(posedge clock) begin
    if(SM3_U0.PAD0.a_block_ready) begin
        $display("INFO : A BLOCK IS PADDED READY !");
        $display("       MSG_PAD_BLOCK = %h",SM3_U0.PAD0.msg_padding_o);
    end
        
end
always@(*) begin
    if(SM3_U0.ECU0.block_comp_finished_out) begin
    #1
        $display("block compress finished!");
        $display("round 63 compress result is: %64h @",SM3_U0.ECU0.Vi_out,$realtime);
        if(SM3_U0.ECU0.Vi_out == 256'h2ad0c8ee_0f92d652_55d90e51_c4f6a60a_69caa1b7_b8ab6d40_f956635a_c6c7ac6c ||(SM3_U0.ECU0.Vi_out == 256'h87ee4178_64f3dc4a_7a9d040f_dff2fa47_af7ee1ee_96e4028f_33e646b4_ea30f510))
            $display("ECU PASS !");
        else
            $display("ERROR ! CHECK COMPRESS VALUE!");
    end
end
always @(*) begin
    if(sm3_finished) begin
        #1
        $display("SM3 PROCESSING FINISHED!");
        $display("HASH VALUE IS %64h",sm3_result);
        if(sm3_result == 256'hdebe9ff9_2275b8a1_38604889_c18e5a4d_6fdb70e5_387e5765_293dcba3_9c0c5732
)
            $display("SM3 TEST PASS !");
        else
            $display("ERROR ! CHECK HASH VALUE!");
    end
end


SM3_Top SM3_U0(
    .clk_i(clock),
    .reset_n_i(rst_n),

    .msg_i(msg),
    .msg_valid_i(msg_valid),
    .is_last_word_in(is_last_word),
    .is_last_word_byte_i(is_last_word_byte),

    .sm3_ready(sm3_ready),
    .sm3_result_out(sm3_result),
    .sm3_finished_out(sm3_finished)
);




endmodule //tb_SM3_Top
